<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
/**
 * RPPAY支付管理控制器
 * @author Fosf
 * @version 1.0
 * @created 2011-04-11
 */

class westernunion extends general
{
    var $orders_status;

    public function __construct()
    { // 公用
        parent::__construct(); // 这是必须的
        $this->URL = $_SERVER['REQUEST_URI'];
    }

    
    //RPPAY支付    接收返回数据，并返回给客户看
    function index()
    {
        //dump($this->spArgs());
        //dump($this->orders_status);
        //exit;
        //$referer=$this->spArgs('referer');
        $siteid=$this->spArgs('siteid');//本站接口方提供的站点ID
        $order_sn=$this->spArgs('order_sn');//本站定单号
        $total=$this->spArgs('total');//交易的总金额
        $verifyCode=$this->spArgs('verifyCode');//交易验证码
        $verified='rp'.urldecode($this->spArgs('verified'));//交易状态
        $currency=$this->spArgs('currency');//交易币种
        $transactionid=$this->spArgs('transactionid');//支付接口方的交易编号
        $payment=$this->spArgs('payment');//支付方式
        
        //根据返回的交易验证，确定是否与本地相配
        if($order_sn)
        {
            $_SESSION['orders_id']='';
            $_SESSION['orders_sn']='';

            $verified=4;                

        
            //echo $verified2;
            
            $conditions = array("orders_sn" => $order_sn);
            //检查定单是否存在
            $order=spClass('orderModel')->find($conditions);
            if($order)
            {
            //存在就修改订单数据
            $data=array('orders_status'=>$verified,'last_modified'=>time());
            spClass('orderModel')->update_order($data,$order_sn,$order['orders_id']);
            
            //订单的交易过程
            $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'date_added'=>time());
            spClass('orders_status_historyModel')->Create($data2);
            
            //如果为支付成功就删除购物车,并发通知邮件
            $status_approve=array('100','106','110','4');
            if(in_array($verified,$status_approve)){
                $customers_basket = spClass('customers_basketModel');
                $customers_id = $this->userarr['customers_id']; //此数据来自初始化
                $conditions = array('customers_id' => $customers_id);
                $customers_basket->delete($conditions); // 删除记录
                //这里将放置付款成功提醒邮件
                    $ordermail = spClass('email_templateModel')->order_send_westernumion($order_sn);//发送订单邮件
                    if ($ordermail)
                    {
                        $sendmailok = true;
                    }
            }
            
            $this->jump(spUrl('main','CheckoutSuccess').'?code=westernumion');
            }else{
                //echo "update status error";
                //订单不存在
                $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
                exit;
            }
        }else{
            //echo "update status error";
            //交易验证失败
            $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
            exit;
        }
    }
}